package com.example.cephdemo.demos.handler;


import com.example.cephdemo.demos.base.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author winterchen
 * @version 1.0
 * @date 2022/3/30 14:25
 * @description 统一处理返回结果集
 **/
@RestControllerAdvice
@Slf4j
public class ResultResponseBodyAdvice {

    // 全局异常拦截（拦截项目中的所有异常）
    @ExceptionHandler
    public Result handlerExceptions(Exception e, HttpServletRequest request, HttpServletResponse response) {
        String message = e.getMessage();
        log.error(message);
        return Result.error(500, message);
    }

}